﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 贪吃蛇试玩demo
{
    
    public partial class FormMain : Form
    {
        private Palette p;
        static public int 方块大小 = 20;
        static public int 页面大小 = 15;

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //初始化
            初始化();

            p.Start();

            p.timerBlock.Stop();
        }

        public void 初始化()
        {
            int width, height, size;
            width = height = 方块大小;
            size = 页面大小;

            this.pictureBox1.Width = width* size;
            this.pictureBox1.Height = height* size;
            this.Width = pictureBox1.Width + 130;
            this.Height = pictureBox1.Height + 64;

            p = new Palette(width, height, size, this.pictureBox1.BackColor, Graphics.FromHwnd(this.pictureBox1.Handle), 5);
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //更新游戏画面
            if (p != null)
            {
                p.PaintPalette(e.Graphics);
            }
        }

        private void FormMain_KeyDown(object sender, KeyEventArgs e)
        {
            int t = p._level;
            速度文本(t);
            t = p._blocks.Count - 1;
            得分文本(t);
            //响应用户按键
            if ((e.KeyCode==Keys.W||e.KeyCode==Keys.Up)&& p._direction != Direction.Down)
            {
                p._direction = Direction.Up;
                return;
            }
            if ((e.KeyCode == Keys.D || e.KeyCode == Keys.Right) && p._direction != Direction.Left)
            {
                p._direction = Direction.Right;
                return;
            }
            if ((e.KeyCode == Keys.S || e.KeyCode == Keys.Down) && p._direction != Direction.Up)
            {
                p._direction = Direction.Down;
                return;
            }
            if ((e.KeyCode == Keys.A || e.KeyCode == Keys.Left) && p._direction != Direction.Right)
            {
                p._direction = Direction.Left;
                return;
            }
        }

        private void 重开()
        {
            重新开始.Visible = true;
            帮助.Visible = true;
        }

        public void 重新开始_Click(object sender, EventArgs e)
        {
            if (p.timerBlock.Enabled == true)
            {
                p.timerBlock.Stop();
                p.timerBlock.Dispose();
            }
            初始化();
            p.Start();
            难度.Text = "难度：" + 0;
            分数.Text = "分数：" + 0;
            暂停.Text = "暂停";
        }

        private void 帮助_Click(object sender, EventArgs e)
        {
            about ab = new about();
            ab.Show();
        }

        private void 暂停_Click(object sender, EventArgs e)
        {
            if (p.timerBlock.Enabled == true)
            {
                p.timerBlock.Stop();
                暂停.Text = "开始";
            }
            else
            {
                p.timerBlock.Start();
                暂停.Text = "暂停";
            }
        }

        private void 退出_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 开始_Click(object sender, EventArgs e)
        {
            pictureBox1.Focus();
            p.timerBlock.Start();
            开始.Visible = false;
            暂停.Visible = true;
            重新开始.Visible = true;
        }

        public void 速度文本(int 等级)
        {
            难度.Text = "难度：" + 等级;
        }

        public void 得分文本(int 数量)
        {
            分数.Text = "分数：" + 数量;
        }

        private void 开始_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                e.IsInputKey = true;
        }

        private void 暂停_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                e.IsInputKey = true;
        }

        private void 重新开始_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                e.IsInputKey = true;
        }

        private void 退出_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                e.IsInputKey = true;
        }

        private void 帮助_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                e.IsInputKey = true;
        }
    }
}
